define(['app'],function(app){
    app.controller('TableCtrl' ,['$scope','$rootScope', '$timeout', '$http','$q','demoService',
       function ($scope, $rootScope, $timeout,$http, $q, demoService) {  
        
        //代码书写步骤
        //1.用【$scope】定义公共类model,和页面绑定的model
        //2.用【var】定义controller内部用的变量
        //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
        //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
        //定义规则，按可用性分忧优先级，公共的在前，private的在后
        
        //初始化listModel
        $scope.tableListData = [];
        $scope.bookModel = {
            "bookId": "",
            "bookName": "",
            "bookType": "",
            "price": 0,//价格
            "publicationYear": "",//出版年份
            "press": "",//出版社
            "introduction": ""
        };

        /**
         * 查询tableList数据
         * @return {[object]} [返回table列表数据]
         */
        $scope.retrTableList = function(){
            // $scope.global.isBusy = true;//暂时没有
            demoService.retrTableList().then(
                function(response){
                    console.log(response);
                    console.log(response);
                    // $scope.global.isBusy = false;
                    if(response.success){
                        $scope.tableListData = response.rows;
                     }else{
                        console.log(response);
                        alert("查询数据失败!");
                     }
                },
                function(code){
                    alert("系统出现问题了！");
                }
            );
        };


        $scope.saveTableList = function(){
            // $scope.global.isBusy = true;
            var saveData = angular.copy($scope.bookModel);//此处要保存的数据应根据具体业务去获取
            demoService.saveTableList(saveData).then(
                function(response){
                    // $scope.global.isBusy = false;
                    if(response.success){
                        alert(response.message);
                        //刷新页面 调用当前页面查询方法即可
                     }else{
                        alert(response.message);
                     }
                },
                function(code){
                    alert("系统出现问题了！");
                }
            );
        };

        /**
         * [removeTableList 删除选中行数据]
         * @param  {[type]} bookId [description]
         * @return {[type]}        [description]
         */
        $scope.removeTableList = function(bookId){
            // $scope.global.isBusy = true;
            console.log(bookId);
            demoService.removeTableList(bookId).then(
                function(response){
                    // $scope.global.isBusy = false;
                    if(response.success){
                        alert(response.message);
                     }else{
                        alert(response.message);
                     }
                },
                function(code){
                    alert("系统出现问题了！");
                }
            );
        };

        var init = function () {
            console.log("grid init");
            $scope.dashboardModel.isActiveMenu = 'grid';//左侧菜单按钮赋初值
            $scope.retrTableList();//初始加载table数据
        };

        init();
    }]);
});